package com.nobug;

import com.sun.xml.internal.bind.v2.model.core.MaybeElement;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static com.nobug.CaptureScreen.getFileSuffix;

/**
 * @author 389561407@qq.com
 * @version 1.0
 * @since 2022-12-28
 */
public class FindImageUtil {


    public static void main(String[] args) throws IOException, AWTException {
        //获取样本
//        String image = CaptureScreen.createImage(CaptureScreen.getImageXYWH(40));


        System.out.println("3秒后扫描桌面");
        MyRobot.sleep(3000);

        //通过样本从显示器找出 n个匹配度高的图片坐标
        File file = new File("C:\\Users\\Administrator\\Desktop\\testImage\\6.png");
        List<MyImageData> bufferedImageList = getBufferedImageList(file);
        System.out.println("匹配到符合条件的数量："+bufferedImageList.size());
        for (MyImageData myImageData : bufferedImageList) {
            int x = myImageData.getX();
            int y = myImageData.getY();
//            System.out.println("x:"+x+"Y:"+y);
            ImageIO.write(myImageData.getBufferedImage(), "png", new File("test", myImageData.getMatch()+".png"));

        }
        for (MyImageData myImageData : bufferedImageList) {
            int[] ins =  getXYByImage(myImageData,file);
            MyRobot.move(ins[0],ins[1]);
        }



    }

    private static int[] getXYByImage(MyImageData myImageData, File file) {
        BufferedImage read = null;
        try {
            read = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        int resX = 0;
        int resY = 0;

        int len = 5;
        double matchCache = 0;

        //精细匹配找到定位
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();    //电脑屏幕大小
            Rectangle screenRect = null;//截图的宽高
            BufferedImage image = null; //暂存图片的缓存
            Robot robot = null;         //负责截屏的操作者
            try {
                robot = new Robot();
            } catch (AWTException e) {
                e.printStackTrace();
            }
            int x = myImageData.getX();
            int y = myImageData.getY();
        System.out.println("X开始扫描："+x);
        System.out.println("Y开始扫描："+y);

            //截图尺寸确定
            screen.width = read.getWidth();
            screen.height = read.getHeight();
            screenRect = new Rectangle(screen);
            int xOffset = myImageData.getWidth()-screen.width+x;
            int yOffset = myImageData.getHeight()-screen.height+y;


            for (int i = x; i < xOffset; i+=len) {
                for (int j = y; j < yOffset; j+=len) {
                    //左上角得坐标
                    screenRect.x= j;
                    screenRect.y= i;
                    //将得到的屏幕信息存放在缓存里面
                    image = robot.createScreenCapture(screenRect);
                    double match = ImageHistogramUtil.match(image, read);
                    if(match > matchCache){
//                        System.out.println("找到匹配度更高的");
                        matchCache = match;
                        resX = j;
                        resY = i;
                    }
                    if(match > 1){
//                        System.out.println("匹配到的x："+resX);
//                        System.out.println("匹配到的y："+resY);
                        try {
                            ImageIO.write(image, "png", new File("test", matchCache+".png"));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        return new int[]{resX,resY};
                    }

                }

            }
//            System.out.println("匹配到的x："+resX);
//            System.out.println("匹配到的y："+resY);
            try {
                ImageIO.write(image, "png", new File("test", matchCache+".png"));
            } catch (IOException e) {
                e.printStackTrace();
            }

        return new int[]{resX,resY};
    }


    //比图片大两倍去查找匹配，返回中所有结果在进行精细匹配
    private static List<MyImageData> getBufferedImageList(File file) {
        BufferedImage read = null;
        try {
            read = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<MyImageData> myImageDataList = new ArrayList<>();
        Robot robot = null;
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); //获取显示器
        //显示器大小
        int height = screen.height;
        int width = screen.width;

        //图片大小
        int widthC = read.getWidth()*2;
        int heightC = read.getHeight()*2;


        //设置截取图片的宽和高
        int x = width/widthC; //切分系数
        int y = height/heightC; //切分系数


        int XM = width/++x;//将要截取的图片大小
        int YM = height/++y; //移动的偏移量


        screen.height = heightC;
        screen.width = widthC;

        Rectangle rectangle = new Rectangle(screen);

        double matchCache  = 0;

        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                //左上角得坐标 上向下 后左向右截取
                rectangle.x= XM*i;
                rectangle.y= YM*j;
                BufferedImage bufferedImage = robot.createScreenCapture(rectangle);

                double match = ImageHistogramUtil.match(bufferedImage, file);
                myImageDataList.add(new MyImageData(rectangle.x,rectangle.y,heightC,heightC,bufferedImage,match));
//                if(match > matchCache){
//                    matchCache = match;
//                    try {
//                        ImageIO.write(bufferedImage, "png", new File("test", match+".png"));
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }

            }
        }


        Collections.sort(myImageDataList, new Comparator<MyImageData>() {
            @Override
            public int compare(MyImageData o1, MyImageData o2) {
                return  o1.getMatch()-o2.getMatch()<0?1:-1;
            }
        });

        return myImageDataList.subList(0,3);
    }

}
